package wwy.Service;

import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import wwy.Common.LogicService;
import wwy.Dao.RoleDao;
import wwy.entity.Role;

@Service
public class RoleService extends LogicService<RoleDao, Role, Long> {
    public RoleService(@Autowired RoleDao dao){super(dao);}

    @Transactional
    public Role getRoleByName(String roleName){
        return dao.getRoleByRoleName(roleName);
    }

    //添加默认角色
    @Transactional
    @EventListener(ApplicationReadyEvent.class)
    @Order(1)
    public void addDefaultRole(){
        if (dao.count() > 0) return;
        Role admin = new Role();
        admin.setRoleName("admin");
        dao.save(admin);
        Role user = new Role();
        user.setRoleName("user");
        dao.save(user);
    }
}

